
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.AuthBo;
import com.wyzz.hr.dao.AuthDao;
import com.wyzz.hr.pojo.Authority;

public class AuthBoImpl implements AuthBo {

	public AuthBoImpl() {
	}

	public List getAuthList() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Authority.class);
		detachedCriteria.addOrder(Order.asc("authorityModuleNo"));
		detachedCriteria.addOrder(Order.desc("authorityConditionNo"));
		return authDao.findByCriteria(detachedCriteria);
	}

	public Authority findAuthById(int id) {
		return (Authority) authDao.loadObject(com.wyzz.hr.pojo.Authority.class,
				Integer.valueOf(id), null, new boolean[0]);
	}

	public boolean updateAuth(Authority authority, Integer actionIds[]) {
		StringBuffer actionId = new StringBuffer();
		if (actionIds != null && actionIds.length > 0) {
			int i = 0;
			do {
				if (i >= actionIds.length)
					break;
				actionId.append(actionIds[i].toString());
				if (i == actionIds.length - 1)
					break;
				actionId.append(",");
				i++;
			} while (true);
		}
		authority.setAuthorityActionNo(actionId.toString());
		try {
			authDao.updateObject(authority);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List findAuthByIdList(List idList) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Authority.class);
		detachedCriteria.add(Restrictions.in("id", idList));
		List findByCriteria = authDao.findByCriteria(detachedCriteria);
		return findByCriteria;
	}

	public Authority getAuthorityId(String moduleNo, String conditionNo) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Authority.class);
		if (moduleNo != null && !moduleNo.trim().equals(""))
			detachedCriteria.add(Restrictions.eq(
					Authority.PROP_AUTHORITY_MODULE_NO, moduleNo));
		if (conditionNo != null && !conditionNo.trim().equals("")) {
			Integer con = new Integer(conditionNo);
			detachedCriteria.add(Restrictions.eq(
					Authority.PROP_AUTHORITY_CONDITION_NO, con));
		}
		List findByCriteria = authDao.findByCriteria(detachedCriteria);
		if (findByCriteria == null || findByCriteria.size() < 1)
			return null;
		else
			return (Authority) findByCriteria.get(0);
	}

	public AuthDao getAuthDao() {
		return authDao;
	}

	public void setAuthDao(AuthDao authDao) {
		this.authDao = authDao;
	}

	public List getAuthList(String modulePrifex) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Authority.class);
		detachedCriteria.add(Restrictions.like("authorityModuleNo",
				modulePrifex, MatchMode.START));
		return authDao.findByCriteria(detachedCriteria);
	}

	private AuthDao authDao;
}
